package com.lanou3g.mynewstest.module.person;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.base.MyBaseFragment;
import com.lanou3g.mynewstest.tools.CircleImageView;
import com.lanou3g.mynewstest.tools.MyRequestQueue;
import com.mob.tools.utils.UIHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * Created by dllo on 16/1/7.
 */
public class PersonFragment extends MyBaseFragment {
    private TextView textView;
    private int[] textViewID = {R.id.yuedu, R.id.shoucang, R.id.gentie, R.id.jinbi};
    private List<TextView> textViews;
    private ListView listView;
    private PersonAdapter adapter;
    private CircleImageView imageView;
    private TextView name;
    private Handler handler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person, container, false);
        ShareSDK.initSDK(getActivity());
        name = (TextView) view.findViewById(R.id.qq_person_name_tv);
        imageView = (CircleImageView) view.findViewById(R.id.person_iv);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        textViews = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            textView = (TextView) view.findViewById(textViewID[i]);
            textView.setText("0");
            textViews.add(textView);
        }
        adapter = new PersonAdapter(context);
        listView = (ListView) view.findViewById(R.id.person_lv);
        listView.setAdapter(adapter);
        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what==998){
                    MyRequestQueue.setNetImage(imageView, (String) msg.obj);
                }else if (msg.what==999){
                    name.setText((CharSequence) msg.obj);
                }
                return false;
            }
        });
        return view;
    }

    public void login() {

        PlatformActionListener listener = new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.d("TrainFragment", platform.getDb().getUserId());
                Log.d("TrainFragment", platform.getDb().getUserName());
                Log.d("TrainFragment", "授权成功");
                Message msg1=new Message();
                msg1.obj=platform.getDb().getUserName();
                msg1.what=999;
                Message msg2=new Message();
                msg2.obj=platform.getDb().getUserIcon();
                msg2.what=998;
                handler.sendMessage(msg1);
                handler.sendMessage(msg2);

            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        };
        Platform platform = ShareSDK.getPlatform(context, QQ.NAME);
        //判断是否已经授权过，如果授权，删除授权信息重新授权
//        if (platform.isValid()) {
//            platform.removeAccount();
//        }
        platform.setPlatformActionListener(listener);
        platform.authorize();


    }
}
